<?php

namespace app\modules\admin\models;

use Yii;

/**
 * This is the model class for table "admin_user".
 *
 * @property int $id
 * @property string $user_id 用户id
 * @property string $username 用户名
 * @property string $true_name
 * @property string $password 密码
 * @property string $email 邮箱
 * @property int $deleted 是否删除
 * @property string $created 创建时间
 * @property string $modified 修改时间
 */
class AdminUser extends BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'admin_user';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'username', 'true_name', 'password'], 'required'],
            [['deleted'], 'integer'],
            [['created', 'modified'], 'safe'],
            [['user_id'], 'string', 'max' => 45],
            [['username'], 'string', 'max' => 16],
            [['true_name'], 'string', 'max' => 20],
            [['password'], 'string', 'max' => 50],
            [['email'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'user_id' => Yii::t('app', '用户id'),
            'username' => Yii::t('app', '用户名'),
            'true_name' => Yii::t('app', 'True Name'),
            'password' => Yii::t('app', '密码'),
            'email' => Yii::t('app', '邮箱'),
            'deleted' => Yii::t('app', '是否删除'),
            'created' => Yii::t('app', '创建时间'),
            'modified' => Yii::t('app', '修改时间'),
        ];
    }

    /**
     * 验证密码
     * @param type $username
     * @param type $password
     * @return boolean
     */
    public function validatePassword($username, $password)
    {
        $user = self::find()->andWhere(array('username' => $username))->one();
        if (!$user) {
            $this->setMessage('用户名不存在');
            return FALSE;
        }

        if ($user['password'] !== sha1($password)) {
            $this->setMessage('密码错误');
            return FALSE;
        }
        return TRUE;
    }

    public function getUserInfo($username, $field = 'id,user_id,true_name,username,email,created,modified')
    {
        $user = self::find()->select($field)->andWhere(array('username' => $username))->asArray()->one();
        return $user;
    }

    /**
     * 生成userId
     * @return type
     */
    public function getUserId()
    {
        $result = self::find()->raw_query('select uuid() as user_id')->asArray()->one();
        if ($result) {
            return current($result);
        } else {
            return FALSE;
        }
    }

    public function getUserList($offset, $limit, $isCount = FALSE, $field = '*')
    {
        $obj = self::find();
        if ($isCount) {
            $result = $obj->count();
        } else {
            $result = $obj->offset($offset)
                ->limit($limit)
                ->select($field)
                ->asArray()->all();
        }
        return $result;
    }
}
